<template>
  <div class="sys-dict-type">

    <AppMainHeader>
      <el-row>
        <el-col :span="22">
          <SysDictQueryForm @query="handleQuery"/>
        </el-col>
        <el-col :span="2" class="align-right">
          <el-button type="primary" @click="handleAdd" v-hasPermi="['sys:dictType:add']">新增</el-button>
        </el-col>
      </el-row>
    </AppMainHeader>

    <AppMainContent>
      <SysDictTable
        v-loading="loading"
        :data="typeList"
        @row-update="handleUpdate"
        @row-delete="handleDelete"
      />

      <Pagination
        v-show="total > 0"
        :total="total"
        :current.sync="queryParams.pageNum"
        :size.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </AppMainContent>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="85px">
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="form.dictName" placeholder="请输入字典名称"/>
        </el-form-item>
        <el-form-item label="字典类型" prop="dictType">
          <el-input v-model="form.dictType" placeholder="请输入字典类型"/>
        </el-form-item>
        <el-form-item label="系统内置" prop="isSystem">
          <el-radio-group v-model="form.isSystem">
            <el-radio
              v-for="item in isSystemOptions"
              :key="item.label"
              :label="item.value"
            >
              {{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入备注"
            resize="none"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// * API 接口
import { addDictType, deleteDictType, pageDictType, updateDictType, fetchDictType } from '@/api/system/dict/type'

// * 页面接口
import SysDictQueryForm from '@/views/system/components/sys-dict-query-form'
import SysDictTable from '@/views/system/components/sys-dict-table'

let pageParams = { pageNum: 1, pageSize: 10 }

export default {
  name: 'SysDict',
  components: { SysDictQueryForm, SysDictTable },
  data() {
    return {
      // * 是否系统内置
      isSystemOptions: [
        { label: "否", value: 0 },
        { label: "是", value: 1 }
      ],
      // * 遮罩层
      loading: false,
      // * 查询参数
      queryParams: { ...pageParams },
      // * 总条数
      total: 0,
      // * 字典表格数据
      typeList: [],

      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dictName: [
          { required: true, message: '字典名称不能为空', trigger: 'blur' },
          { max: 25, message: '长度不超过25个字符', trigger: 'blur' }
        ],
        dictType: [
          { required: true, message: '字典类型不能为空', trigger: 'blur' },
          { max: 25, message: '长度不超过25个字符', trigger: 'blur' }
        ],
        remark: [{ max: 25, message: '长度不超过25个字符', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询字典类型列表 */
    getList() {
      this.loading = true

      pageDictType(this.queryParams).then((response) => {
        let { data = [], page = {} } = response
        let { pageNum = 1, pageSize = 10, total = 0 } = page

        data.forEach((item, index) => {
          item.rowNumber = (pageNum - 1) * pageSize + index + 1
        })

        this.typeList = data
        this.total = total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        dictId: undefined,
        dictName: undefined,
        dictType: undefined,
        isSystem: 0,
        remark: undefined
      }
      this.resetForm('form')
    },
    // * 操作：表单查询
    handleQuery(queryForm = {}) {
      this.queryParams = {...queryForm, ...pageParams }

      this.getList()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加字典类型'
    },
    /** todo 修改按钮操作 */
    handleUpdate(row) {
      this.reset()

      fetchDictType(row.dictId).then((response) => {
        let { data } = response
        if (data.isSystem && typeof data.isSystem === 'object') {
          data.isSystem = data.isSystem.value || 0
        }

        this.form = data
        this.open = true
        this.title = '修改字典类型'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.dictId) {
            updateDictType(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addDictType(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** TODO 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        '是否确认删除字典名称为"' + row.dictName + '"的数据项?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          return deleteDictType(row.dictId)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
    },
  }
}
</script>

<style lang="scss" scoped>

</style>
